#include "timer.h"

void TIME5_Int_Init(u32 arr,u16 psc)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
	
	TIM_TimeBaseInitStruct.TIM_Period=arr;//自动重装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler=psc;//预分频系数
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//计数器模式向上计数
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//时钟分割
	TIM_TimeBaseInit(TIM5, &TIM_TimeBaseInitStruct);
	TIM_ITConfig(TIM5, TIM_IT_Update,ENABLE);//使能定时器的更新中断
	

	NVIC_InitStruct.NVIC_IRQChannel=TIM5_IRQn;//通道号
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
	//NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
	NVIC_Init(&NVIC_InitStruct);
	//TIM_Cmd(TIM5,ENABLE);//开启定时器5
	
}
void TIME6_Int_Init(u32 arr,u16 psc)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE);
	
	TIM_TimeBaseInitStruct.TIM_Period=arr;//自动重装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler=psc;//预分频系数
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//计数器模式向上计数
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//时钟分割
	TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStruct);
	TIM_ITConfig(TIM6, TIM_IT_Update,ENABLE);//使能定时器的更新中断
	

	NVIC_InitStruct.NVIC_IRQChannel=TIM6_DAC_IRQn;//通道号
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=3;
	//NVIC_InitStruct.NVIC_IRQChannelSubPriority=;
	NVIC_Init(&NVIC_InitStruct);
	TIM_Cmd(TIM6,ENABLE);//开启定时器6
	
}
void TIME3_Int_Init(u32 arr,u16 psc)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	
	TIM_TimeBaseInitStruct.TIM_Period=arr;//自动重装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler=psc;//预分频系数
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//计数器模式向上计数
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//时钟分割
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
	TIM_ITConfig(TIM3, TIM_IT_Update,ENABLE);//使能定时器的更新中断
	

	NVIC_InitStruct.NVIC_IRQChannel=TIM3_IRQn;//通道号
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
	//NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
	NVIC_Init(&NVIC_InitStruct);
	TIM_Cmd(TIM3,ENABLE);//开启定时器6
	
}
void TIM7_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);

	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period=4-1;
	TIM_TimeBaseInitStruct.TIM_Prescaler=21-1;
	TIM_TimeBaseInit(TIM7, &TIM_TimeBaseInitStruct);
}
void delay_us(u16 times)
{
	u32 us=times;
	TIM_Cmd(TIM7, ENABLE);
	while(us--){
		while(TIM_GetFlagStatus(TIM7, TIM_FLAG_Update)==RESET);
		TIM_ClearFlag(TIM7, TIM_FLAG_Update);
	}	
	TIM_Cmd(TIM7, DISABLE);
}

void delay_ms(u16 times)
{
	u32 ms=times*1000;
	TIM_Cmd(TIM7, ENABLE);
	while(ms--){
		while(TIM_GetFlagStatus(TIM7, TIM_FLAG_Update)==RESET);
		TIM_ClearFlag(TIM7, TIM_FLAG_Update);
	}	
	TIM_Cmd(TIM7, DISABLE);
}

